package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.RegionQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private IRegionService regionService;

    @RequestMapping("/list")
    public String list(){
        return "region/list";
    }
    @RequestMapping("/listByParentId")
    @ResponseBody
    public List listByParentId(@ModelAttribute("qo") RegionQueryObject qo,String type){
        //查询地区的集合
        List<Region> list = regionService.listByParentId(qo);
        //根据当前的
        //用来存储treeMap对应属性的集合
        if ("tree".equals(type)) {
            List<Map> maps = new ArrayList<>();
            for (Region region : list) {
                //把每一个地区都转换成map
                maps.add(region.toTreeMap());
            }
            return maps;
        }
        return list;
    }
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Region entity){

        regionService.saveOrUpdate(entity);

        return new JSONResult();
    }
    @RequestMapping("/changeStatus")
    @ResponseBody
    public JSONResult changeStatus(Long id , Integer state){

        regionService.changeStatus(id,state);

        return new JSONResult();
    }


}
